package controllers

import (
	"Godzilla/common/request"
	"Godzilla/common/responses"
	"Godzilla/enum/code"
	"Godzilla/enum/fileds"
	"Godzilla/models"
	"Godzilla/repositories"
	"github.com/labstack/echo/v4"
	"net/http"
	"strconv"
	"unicode/utf8"
)


func SaveUser(c echo.Context)  error {
	var (
		username 	string
		password    string
	)

	u := new(models.User)
	if err := c.Bind(u); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}


	//username = c.FormValue("username")
	//password = c.FormValue("password")
	username = u.UserName
	password = u.HashPassword

	if username == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "用户名不能为空", username))
	}

	if password == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "密码不能为空", password))
	}

	if utf8.RuneCountInString(username) < 5 || utf8.RuneCountInString(username) > 10 {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "用户名不能小于5大于10字符", ""))
	}

	if utf8.RuneCountInString(password) < 5 || utf8.RuneCountInString(password) > 10 {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "密码不能小于5大于10", ""))
	}

	userManager, err := repositories.NewUserManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.ConnectError, "连接错误", err.Error()))
	}

	id, err := userManager.InsertUser(username, password)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err))
	}


	return c.JSON(http.StatusOK, responses.Response(200, "成功", id))
}

func GetUser(c echo.Context) error {
	var (
		id string
	)

	id = c.Param("id")
	if id == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError,"id不能为空", ""))
	}

	userManager, err := repositories.NewUserManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.ConnectError, "连接错误", err.Error()))
	}
	user, error := userManager.SelectUserById(id)
	if error != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, error.Error(), ""))
	}
	return c.JSON(http.StatusOK, responses.Response(200, "成功",user))
}

func UpdateUser(c echo.Context) error {
	var (
		getId       string
		id 			int64
		username 	string
		err			error
	)

	getId = c.Param("id")
	if getId == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError,"id不能为空", ""))
	}

	id, err = strconv.ParseInt(getId, 10, 64)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError,err.Error(), ""))
	}

	u := new(models.User)
	if err := c.Bind(u); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}
	username = u.UserName

	if utf8.RuneCountInString(username) < 5 || utf8.RuneCountInString(username) > 10 {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "用户名不能小于5大于10", ""))
	}

	userManager, err := repositories.NewUserManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.ConnectError, "连接错误", err.Error()))
	}

	err = userManager.UpdateUserById(id, username)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ServerError, err.Error(), ""))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", ""))
}

func DeleteUser(c echo.Context) error {
	var (
		getId       string
		id 			int64
		err			error
	)
	getId = c.Param("id")
	if getId == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError,"id不能为空", ""))
	}

	id, err = strconv.ParseInt(getId, 10, 64)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError,err.Error(), ""))
	}

	userManager, err := repositories.NewUserManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.ConnectError, "连接错误", err.Error()))
	}
	_ , err = userManager.DeleteUserById(id)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, err.Error(), ""))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", ""))
}

func GetUsers(c echo.Context) error {
	var (
		page	  int64
		size  	  int64
		name      string
		where     string
		sort	  string
		filed     string
	)

	u := new(request.User)
	if err := c.Bind(u); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}
	page = u.Page
	size = u.Size
	name = u.Name
	sort = u.Sort
	filed = u.SortFile

	if page == 0 {
		page = 1
	}
	if size == 0 {
		size = 10
	}
	if !request.ValidateUserSort(sort) {
		sort = fileds.ASC
	}
	if !request.ValidateUserSortFile(filed) {
		filed = fileds.ID
	}
	where = " status = 1 "
	if name != "" {
		where += " AND nickname LIKE '%" +name+ "%'"
	}

	userManager, err := repositories.NewUserManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	resultList , err :=  userManager.SelectUsers(where, page, size, sort, filed)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "sql错误", err.Error()))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", resultList))
}